home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-06 | 37.0 KB | 951 lines | [TEXT/SPM ] |
- /*
- IC Link In.c
-
- Main code for accessing the configuration file. This code can be linked in to an application
- (if the component is not available) to provide access to the IC prefs file.
-
- This code represents the lowest-level of access to the config file.
-
- History
- 11/06/95 dhn - Started C port.
- */
-
- /*
- Original pascal comment:
-
- (* File: ICLinkIn.p
- * Generated by: 1.0d4
- * For: IC 1.2
- * On: Monday, 25 September 1995, 19:28:12
- *
- * This file is part of the Internet Configuration system and
- * is placed in the public domain for the benefit of all.
- *)
- */
-
- #include <Components.h>
- #include <AppleTalk.h>
- #include <Folders.h>
- #include <Processes.h>
- #include <Errors.h>
- #include <AppleEvents.h>
- #include <LowMem.h>
-
- #include "IC Types.h"
- #include "IC Keys.h"
-
- #include "IC Resource API.h"
- #include "IC Link In Subs.h"
-
- #define kICOurManufacturer 'JPQE'
- #define kRes_Code 'ICRP'
-
- // Local Prototypes
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- OSErr ICFindFolder(short vRefNum,OSType folderType,Boolean createFolder,short* foundVRefNum,long* foundDirID);
- Boolean ICRCloseIfOpen(ICRRecordPtr inst);
- ICError ValidDirSpec(ICDirSpec* folder);
- OSErr FoundFile(ICDirSpec* folder,short ndx,FSSpec* found_file);
- Boolean ScanFolder(ICRRecordPtr inst,ICDirSpec* folder,FSSpecPtr found_file);
- OSErr FindPrefFolder(ICDirSpec* pref_fold);
- void SetSFCWD(ICRRecordPtr inst);
- ICError GetFile(ICRRecordPtr inst,FSSpecPtr fs);
- ICError PutFile(ICRRecord* inst,FSSpecPtr fs);
- short ICRPermToFSPerm(ICPerm perm);
- ICError ICRCheckInside(ICRRecord* inst);
- ICError ICRForceInside(ICRRecord* inst,ICPerm perm,Boolean* force_info);
- ICError ICRReleaseInside(ICRRecord* inst,Boolean force_info);
- Boolean __URL_SpecStartChar(char ch);
- Boolean __URL_SpecEndChar(char ch);
- ICError ExpandSelection(char* datap,long len,long* selStart,long* selEnd);
- Boolean SpaceTab(char ch);
- Boolean SpaceTabRet(char ch);
- ICError ShrinkSelection(char* datap,long len,long* selStart,long* selEnd);
- ICError StripReturns(Handle urlh);
- void Unpa h=(Handle)0;
- }
- if (prefh==(Handle)0)
- old_attr=0;
- else
- old_attr=**((long**)prefh);
-
- if (attr==ICattr_no_change)
- attr=old_attr;
-
- if ((old_attr&ICattr_locked_mask)&&(attr&ICattr_locked_mask)&&(buf!=(Ptr)0))
- err=icPermErr;
-
- if (prefh==(Handle)0){
- prefh=NewHandle(size+4);
- err=MemError();
-
- if (err==noErr){
- do {
- id=Unique1ID(kRes_Code);
- } while (id<=127);
-
- AddResource(prefh,kRes_Code,id,key);
- err=ResError();
-
- if (err!=noErr){
- DisposeHandle(prefh);
- prefh=(Handle)0;
- }
- }
- }
- if ((err==noErr)&&(buf!=(Ptr)0)){
- SetHandleSize(prefh,size+4);
- err=MemError();
- }
- if ((err==noErr)&&(size>0))
- BlockMoveData(buf,(Ptr)((*prefh)+4),size);
- if (err==noErr){
- **((long**)prefh)=attr;
- ChangedResource(prefh);
- WriteResource(prefh);
- err=ResError();
- }
- UseResFile(old_refnum);
- }
- }
-
- if (prefh!=(Handle)0)
- ReleaseResource(prefh);
-
- err2=ICRReleaseInside(inst,force_info);
-
- if (err==noErr)
- err=err2;
-
- return err;
- }
-
- // I call ICRForceInside to speed this routine up. ICRForceInside will do an ICRBegin and hence open the resource
- // file, which is good because otherwise I'd open it twice, once for each ICRGetPref.
-
- ICError ICRFindPrefHandle(ICRRecord* inst,StringPtr key,ICAttr* attr,Handle prefh){
- ICError err=noErr,err2=noErr;
- long prefsize=0L;
- Boolean force_info;
-
- if (prefh==(Handle)0)
- err=paramErr;
-
- if (err==noErr){
- err=ICRForceInside(inst,icReadOnlyPerm,&force_info);
- if (err==noErr)
- err=ICRGetPref(inst,key,attr,(Ptr)0,&prefsize);
-
- if (err==noErr){
- SetHandleSize(prefh,prefsize);
- err=MemError();
- }
-
- if (err==noErr){
- HLock(prefh);
- err=ICRGetPref(inst,key,attr,*prefh,&prefsize);
- HUnlock(prefh);
- }
-
- err2=ICRReleaseInside(inst,force_info);
- }
- if (err==noErr)
- err=err2;
-
- if ((prefh!=(Handle)0)&&(err!=noErr)){
- SetHandleSize(prefh,0);
- }
-
- return err;
- }
-
- ICError ICRGetPrefHandle(ICRRecord* inst,StringPtr key,ICAttr* attr,Handle* prefh){
- ICError err;
-
- *prefh=NewHandle(0);
- err=MemError();
-
- if (err==noErr)
- err=ICRFindPrefHandle(inst,key,attr,*prefh);
-
- if (err==icPrefNotFoundErr){
- SetHandleSize(*prefh,0);
- *attr=0;
- err=noErr;
- }
-
- return err;
- }
-
- ICError ICRSetPrefHandle(ICRRecord* inst,StringPtr key,ICAttr attr,Handle prefh){
- ICError err=noErr;
- SignedByte s;
-
- if (prefh!=(Handle)0){
- if (*prefh==(Ptr)0)
- err=paramErr;
- if (err==noErr){
- s=HGetState(prefh);
- HLock(prefh);
- err=ICRSetPref(inst,key,attr,*prefh,GetHandleSize(prefh));
- HSetState(prefh,s);
- }
- } else {
- err=ICRSetPref(inst,key,attr,(Ptr)0,0);
- }
-
- return err;
- }
-
- ICError ICRCountPref(ICRRecord* inst,long* count){
- ICError err;
- short old_refnum;
-
- err=ICRCheckInside(inst);
- if (err==noErr){
- if (inst->config_refnum==0)
- *count=0;
- else {
- old_refnum=CurResFile();
- UseResFile(inst->config_refnum);
- err=ResError();
- if (err==noErr){
- *count=Count1Resources(kRes_Code);
- err=ResError();
- UseResFile(old_refnum);
- }
- }
- }
-
- if (err!=noErr)
- *count=0;
-
- return err;
- }
-
- ICError ICRGetIndPref(ICRRecord* inst,long n,StringPtr key){
- ICError err;
- short old_refnum;
- Handle prefh=(Handle)0;
- short junk_id;
- ResType junk_type;
-
- err=ICRCheckInside(inst);
- if ((err==noErr)&&(n<1))
- err=paramErr;
-
- if (err==noErr){
- if (inst->config_refnum==0)
- err=icPrefNotFoundErr;
- else {
- old_refnum=CurResFile();
- UseResFile(inst->config_refnum);
- err=ResError();
- if (err==noErr){
- SetResLoad(false);
- prefh=Get1IndResource(kRes_Code,n);
- SetResLoad(true);
-
- if (prefh==(Handle)0)
- err=icPrefNotFoundErr;
- else {
- GetResInfo(prefh,&junk_id,&junk_type,key);
- err=ResError();
- }
- UseResFile(old_refnum);
- }
- }
- }
-
- if (prefh!=(Handle)0)
- ReleaseResource(prefh);
-
- return err;
- }
-
- ICError ICRDeletePref(ICRRecord* inst,StringPtr key){
- ICError err;
- Handle prefh;
- short old_refnum;
-
- err=ICRCheckInside(inst);
- if ((err==noErr)&&(key[0]==0))
- err=paramErr;
-
- if (err==noErr){
- if (inst->config_refnum==0)
- err=icPrefNotFoundErr;
- else {
- old_refnum=CurResFile();
- UseResFile(inst->config_refnum);
- err=ResError();
- if (err==noErr){
- SetResLoad(false);
- prefh=Get1NamedResource(kRes_Code,key);
- err=ResError();
- SetResLoad(true);
- if (prefh==(Handle)0)
- err=icPrefNotFoundErr;
- else {
- RmveResource(prefh);
- DisposeHandle(prefh);
- err=ResError();
- }
- UseResFile(old_refnum);
- }
- }
- }
-
- return err;
- }
-
- ICError ICREnd(ICRRecord* inst){
- ICError err;
-
- err=ICRCheckInside(inst);
- ICRCloseIfOpen(inst);
-
- return err;
- }
-
- ICError ICRDefaultFileName(ICRRecord* inst,StringPtr name){
- Str63 lname=kICDefaultFileName;
-
- BlockMoveData(lname,name,lname[0]+1);
-
- return noErr;
- }
-
- ICError ICREditPreferences(ICRRecord* inst,StringPtr key){
- ICError err;
-
- if (!inst->have_config_file)
- return bdNamErr;
-
- return EditPreferences(key,&(inst->config_file));
- }
-
- /*
- URL Parsing Algorithm
-
- 1. if there is a selection skip to step 4
- 2. expand selection to end of word (never skip an angle bracket though)
- 3. if either end has an angle bracket then expand other end to search for angle bracket
- 4. strip trailing and leading whitespace
- 5. strip whitespace CR whitespace
- 6. off < > if necessary
- 7. remove leading URL:
- 8. extract protocol by looking forwards for :
- 9. if no protocol then prepend "hint:"
- */
-
- Boolean __URL_SpecStartChar(char ch){
- switch (ch){
- case ' ':
- case '<':
- case 9:
- case 13:
- return true;
- default:
- return false;
- }
- }
-
- Boolean __URL_SpecEndChar(char ch){
- switch (ch){
- case ' ':
- case '>':
- case 9:
- case 13:
- return true;
- default:
- return false;
- }
- }
-
- ICError ExpandSelection(char* datap,long len,long* selStart,long* selEnd){
- ICError err=noErr;
- Boolean found;
-
- // expand leading selection backwards looking for word break
- while ((*selStart>0)&&(!__URL_SpecStartChar(datap[(*selStart)-1])))
- (*selStart)--;
- if ((*selStart>0)&&(datap[(*selStart)-1]=='<'))
- (*selStart)--;
-
- // expand trailing selection forwards looking for word break
- while ((*selEnd<len)&&(!__URL_SpecEndChar(datap[*selEnd])))
- (*selEnd)++;
- if ((*selEnd<len)&&(datap[*selEnd]=='>'))
- (*selEnd)++;
-
- // if first char was a < then expand trailing selection to meet matching >
- if ((datap[*selStart]=='<')&&(datap[(*selEnd)-1]!='>')){
- while ((datap[(*selEnd)-1]!='>')&&((*selEnd)-1<=len))
- (*selEnd)++;
-
- // now either a match or at end of len
- if (datap[(*selEnd)-1]!='>')
- return icNoURLErr;
- }
-
- // if last char was a > then expand leading selection to meet matching <
- if ((datap[*selEnd]=='>')&&(datap[*selStart]!='<')){
- while ((datap[*selStart]!='<')&&(*selStart>=0))
- (*selStart)--;
-
- // now either a match or at end of len
- if (datap[*selStart]!='<')
- return icNoURLErr;
- }
-
- return err;
- }
-
- Boolean SpaceTab(char ch){
- if ((ch==' ')||(ch==9))
- return true;
- return false;
- }
-
- Boolean SpaceTabRet(char ch){
- if (ch==13)
- return true;
- return SpaceTab(ch);
- }
-
- ICError ShrinkSelection(char* datap,long len,long* selStart,long* selEnd){
- if (SpaceTab(datap[*selStart]))
- while (SpaceTab(datap[*selStart]))
- (*selStart)++;
- if (SpaceTab(datap[(*selEnd)-1]))
- while (SpaceTab(datap[(*selEnd)-1]))
- (*selEnd)--;
-
- return noErr;
- }
-
- ICError StripReturns(Handle urlh){
- ICError err;
- long srcsize,srcndx,dstndx;
- char* buf;
- SignedByte s;
-
- srcsize=GetHandleSize(urlh);
- err=MemError();
- if (srcsize==0){
- if (err!=noErr)
- return err;
-
- return icNoURLErr;
- }
-
- srcndx=0;
- dstndx=0;
-
- s=HGetState(urlh);
- HLock(urlh);
- buf=(char*)(*urlh);
-
- // skip down the buffer copying src to dst except when meeting cr
- while (srcndx<srcsize){
- if (buf[srcndx]==13){
- // move dstndx back to point to previous non-whitespace
- while ((dstndx>0)&&(SpaceTab(buf[dstndx-1])))
- dstndx--;
- // move srcndx forwards to next non-whitespace
- while ((srcndx<srcsize)&&(SpaceTabRet(buf[srcndx])))
- srcndx++;
- }
-
- if (srcndx<srcsize){
- // copy byte from src to dest
- buf[dstndx]=buf[srcndx];
- srcndx++;
- dstndx++;
- }
- }
-
- HSetState(urlh,s);
-
- // resize the handle to the number of bytes that we copied
- SetHandleSize(urlh,dstndx);
- err=MemError();
-
- return err;
- }
-
- ICError ICRParseURL(ICRRecord* inst,StringPtr hint,Ptr data,long len,long* selStart,long* selEnd,Handle urlh){
- char* datap=(char*)data;
- Str31 tmp="\pURL:";
- long junklong;
- long ndx;
- ICError err=noErr;
-
- if ((data==(Ptr)0)||(urlh==(Handle)0)||(*urlh==(Ptr)0)||(len<=0)||(*selStart<0)||(*selEnd<0)||(*selStart>len)||(*selEnd>len)||(*selStart>*selEnd))
- return paramErr;
-
- if (*selStart==*selEnd)
- err=ExpandSelection(datap,len,selStart,selEnd);
- if (err==noErr) // remove leading and trailing whitespace
- err=ShrinkSelection(datap,len,selStart,selEnd);
-
- if ((err==noErr)&&(*selStart>=*selEnd))
- return icNoURLErr;
-
- err=PtrToXHand((Ptr)&(datap[*selStart]),urlh,(*selEnd)-(*selStart));
- if (err==noErr)
- err=StripReturns(urlh);
-
- if (err==noErr){
- // strip any enclosing < >
- char* buf;
- long bsize=GetHandleSize(urlh);
- SignedByte s=HGetState(urlh);
- Boolean doTrim=false;
-
- HLock(urlh);
- buf=(char*)(*urlh);
-
- if ((buf[0]=='<')&&(buf[bsize-1]=='>')){
- doTrim=true;
- }
-
- HSetState(urlh,s);
-
- if (doTrim){
- SetHandleSize(urlh,bsize-1);
- HUnlock(urlh);
- // unlock before the munger call
- Munger(urlh,0,(Ptr)0,1,(Ptr)-1,0);
- HSetState(urlh,s);
- }
-
- // trim off leading "\pURL:"
- HLock(urlh);
- if ((GetHandleSize(urlh)>tmp[0])&&(IUMagIDString(*urlh,&(tmp[1]),tmp[0],tmp[0])==0)){
- HUnlock(urlh);
- Munger(urlh,0,(Ptr)0,tmp[0],(Ptr)-1,0);
- }
- HSetState(urlh,s);
-
- // search for protocol
- tmp[0]=1;tmp[1]=':';
- HUnlock(urlh);
- ndx=Munger(urlh,0,&(tmp[1]),1,(Ptr)0,0);
- if ((ndx<0)||(ndx>255)){
- // failed to find a colon in first 256 bytes,prepend "hint:" to url
- if (hint[0]==0)
- err=icNoURLErr;
- else {
- Munger(urlh,0,(Ptr)0,0,&(hint[1]),hint[0]);
- err=MemError();
- }
- }
- HSetState(urlh,s);
- }
- return err;
- }
-
- ICError ICRLaunchURL(ICRRecord* inst,StringPtr hint,Ptr data,long len,long* selStart,long* selEnd){
- ICError err;
- Handle urlh;
- ICAppSpec helper;
- Str255 scheme;
- Str255 temp=kICHelper;
- long junk_attr;
- long size;
-
- urlh=NewHandle(0);
- err=MemError();
-
- if (err==noErr)
- err=ICRParseURL(inst,hint,data,len,selStart,selEnd,urlh);
- else
- urlh=(Handle)0;
-
- if (err==noErr)
- err=FindScheme(urlh,scheme);
-
- if (err==noErr){
- size=sizeof(ICAppSpec);
- BlockMoveData(scheme,(Ptr)&(temp[temp[0]+1]),scheme[0]);
- temp[0]+=scheme[0];
- err=ICRGetPref(inst,temp,&junk_attr,(Ptr)(&helper),&size);
- }
-
- if (err==noErr)
- err=LaunchURL(helper.fCreator,urlh);
-
- if (urlh!=(Handle)0)
- DisposeHandle(urlh);
-
- return err;
- }
-
- void UnpackCopyString(Ptr* p,StringPtr s){
- short len;
-
- // calc the len of the current pascal string pointed to by p
- len=((**p) & 0x00ff) +1;
-
- // copy the string in the buffer to s
- BlockMoveData(*p,s,len);
-
- // move the pointer to the start of the next pascal string
- *p=(Ptr)((*p)+len);
- }
-
- // Internal Mapping Subs
- OSErr UnpackEntry(Handle entries,long pos,ICMapEntry* entry,long* user_length){
- // WARNING: Depends very much on the exact format of ICMapEntry!
- Ptr org,p;
- long maxsize;
- OSErr err=noErr;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(pos<0)||(pos>GetHandleSize(entries)-6))
- return paramErr;
-
- p=(Ptr)((*entries)+pos);
- maxsize=GetHandleSize(entries);
- org=p;
-
- BlockMoveData(p,(Ptr)entry,6);
-
- if ((entry->fixed_length!=ICmap_fixed_length)||(entry->fixed_length>entry->total_length)||(entry->total_length>maxsize))
- return badExtResource;
-
- BlockMoveData(p,entry,entry->fixed_length);
- p=(Ptr)(p+entry->fixed_length);
- UnpackCopyString(&p,entry->extension);
- UnpackCopyString(&p,entry->creator_app_name);
- UnpackCopyString(&p,entry->post_app_name);
- UnpackCopyString(&p,entry->MIME_type);
- UnpackCopyString(&p,entry->entry_name);
-
- *user_length=entry->total_length-(p-org);
-
- return err;
- }
-
- // a fast version of ICRGetEntry doesn't return all of the strings for the entry
- // WARNING: Depends very much on the exact format of ICMapEntry!
- OSErr FastGetEntry(Handle entries,long pos,ICMapEntry* entry){
- Ptr org,p;
- long maxsize;
- OSErr err=noErr;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(pos<0)||(pos>GetHandleSize(entries)-6))
- return paramErr;
-
- p=(Ptr)((*entries)+pos);
- maxsize=GetHandleSize(entries);
- BlockMoveData(p,entry,6);
-
- if ((entry->fixed_length!=ICmap_fixed_length)||(entry->fixed_length>entry->total_length)||(entry->total_length>maxsize))
- return badExtResource;
-
- BlockMoveData(p,entry,entry->fixed_length);
- p=(Ptr)(p+entry->fixed_length);
- BlockMoveData(p,entry->extension,(((*p)&0x00ff)+1));
- return err;
- }
-
- void PackCopyString(ICMapEntry* entry,Ptr p,StringPtr s){
-
- BlockMoveData(s,(Ptr)(p+entry->total_length),s[0]+1);
- entry->total_length+=s[0]+1;
- }
-
- void PackEntry(ICMapEntry* entry,Ptr p,long user_length){
- Ptr a,b;
-
- entry->version=0;
-
- a=(Ptr)&(entry->extension);
- b=(Ptr)entry;
-
- entry->fixed_length=a-b;
- entry->total_length=entry->fixed_length;
-
- PackCopyString(entry,p,entry->extension);
- PackCopyString(entry,p,entry->creator_app_name);
- PackCopyString(entry,p,entry->post_app_name);
- PackCopyString(entry,p,entry->MIME_type);
- PackCopyString(entry,p,entry->entry_name);
-
- entry->total_length+=user_length;
-
- BlockMoveData(entry,p,entry->fixed_length);
- }
-
- short GetShort(Ptr P){
- unsigned char* p=(unsigned char*)P;
- short val=0;
-
- val=p[0];
- val <<= 8;
- val+=p[1];
-
- return val;
- }
-
- char UpCase(char ch){
-
- if ((ch>='a')&&(ch<='z'))
- return (ch-'a')+'A';
-
- return ch;
- }
-
- Boolean IsExtensionVar(StringPtr name,StringPtr ext){
- short pn,pe;
-
- if (name[0]>=ext[0]){
- pn=name[0]-ext[0]+1;
- pe=1;
-
- while (pe<=ext[0]){
- if (UpCase(name[pn])!=UpCase(ext[pe]))
- break;
-
- pn++;
- pe++;
- }
-
- return pe>ext[0];
- }
- return false;
- }
-
- // Low Level Mapping Routines
-
- ICError ICRCountMapEntries(ICRRecord* inst,Handle entries,long* count){
- Ptr p;
- long pos;
- short size;
- long hsize;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0))
- return paramErr;
-
- p=*entries;
- pos=0L;
- *count=0;
- hsize=GetHandleSize(entries);
- while (pos<hsize){
- size=GetShort(p); // extract the total_length value from the current pointer
- pos+=size; // add it to the pos offset
- p+=size; // add it to the pointer
- (*count)++; // increment the count
- }
-
- return noErr;
- }
-
- ICError ICRGetIndMapEntry(ICRRecord* inst,Handle entries,long ndx,long* pos,ICMapEntry* entry){
- ICError err;
- Ptr p;
- long i;
- short size;
- long hsize;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(ndx<0))
- return paramErr;
-
- p=(Ptr)*entries;
- *pos=0;
- hsize=GetHandleSize(entries);
-
- while ((ndx>1)&&(*pos<hsize)){
- size=GetShort(p); // extract the total_length value from the current pointer
- *pos+=size; // add the size to the pos offset
- p+=size; // add the size to the pointer
- ndx--; // decrease the index for the entry
- }
-
- return ICRGetMapEntry(inst,entries,*pos,entry);
- }
-
- ICError ICRGetMapEntry(ICRRecord* inst,Handle entries,long pos,ICMapEntry* entry){
- ICError err;
- long user_length;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(pos<0)||(pos>GetHandleSize(entries)))
- return paramErr;
-
- return UnpackEntry(entries,pos,entry,&user_length);
- }
-
- ICError ICRSetMapEntry(ICRRecord* inst,Handle entries,long pos,ICMapEntry* entry){
- ICError err;
- ICMapEntry e,oldentry;
- long user_length,source_length;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(pos<0)||(pos>GetHandleSize(entries)))
- return paramErr;
-
- err=UnpackEntry(entries,pos,&oldentry,&user_length);
-
- if (err==noErr){
- PackEntry(entry,(Ptr)&e,user_length);
- source_length=oldentry.total_length-user_length;
-
- if (user_length<8){ // hack to remove alignment bytes from previous version
- source_length=oldentry.total_length;
- e.total_length=e.total_length-user_length;
- user_length=0;
- }
- Munger(entries,pos,(Ptr)0,source_length,&e,e.total_length-user_length);
- err=MemError();
- }
-
- return err;
- }
-
- ICError ICRDeleteMapEntry(ICRRecord* inst,Handle entries,long pos){
- ICError err;
- ICMapEntry entry;
- long user_length;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(pos<0)||(pos>=GetHandleSize(entries)))
- return paramErr;
-
- err=UnpackEntry(entries,pos,&entry,&user_length);
-
- if (err==noErr){
- Munger(entries,pos,(Ptr)0,entry.total_length,(Ptr)-1,0);
- err=MemError();
- }
-
- return err;
- }
-
- ICError ICRAddMapEntry(ICRRecord* inst,Handle entries,ICMapEntry* entry){
- ICError err;
- ICMapEntry tmp_entry;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0))
- return paramErr;
-
- PackEntry(entry,(Ptr)&tmp_entry,0);
-
- return PtrAndHand(&tmp_entry,entries,entry->total_length);
- }
-
- // High Level Mapping Subs
-
- ICError ICRMapEntriesFilename(ICRRecord* inst,Handle entries,StringPtr filename,ICMapEntry* entry){
- // Implementation lifted directly from Space Aliens
- ICError err;
- short longest_len;
- long posndx,found_pos;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0)||(filename[0]==0))
- return paramErr;
-
- // loop through the entries
- // looking for the longest match
-
- longest_len=0;
- posndx=0;
-
- while (FastGetEntry(entries,posndx,entry)==noErr){
- // the entry matches if not_incoming flag bit is clear,
- // it's longer than the previous match, it's longer than the filename,
- // and it matches the last N chars of the filename.
-
- if ((entry->extension[0]>longest_len)&&(!(entry->flags&ICmap_not_incoming_mask))&&(IsExtensionVar(filename,entry->extension))){
- // record the new longest entry
- found_pos=posndx;
- longest_len=entry->extension[0];
- }
- // increment posndx so that we get the next entry the next time around the loop
- posndx+=entry->total_length;
- }
-
- if (longest_len==0)
- return icPrefNotFoundErr;
-
- return ICRGetMapEntry(inst,entries,found_pos,entry);
- }
-
- ICError ICRMapEntriesTypeCreator(ICRRecord* inst,Handle entries,OSType fType,OSType fCreator,StringPtr filename,ICMapEntry* entry){
- ICError err;
- long posndx,found_pos,match_weight,best_weight;
-
- if ((entries==(Handle)0)||(*entries==(Ptr)0))
- return paramErr;
-
- posndx=0L;
- best_weight=-1L;
-
- while (FastGetEntry(entries,posndx,entry)==noErr){
- if (!(entry->flags&ICmap_not_outgoing_mask)){
- if (entry->file_type==fType){
- match_weight=entry->file_creator==fCreator;
- if (IsExtensionVar(filename,entry->extension)){
- match_weight+=2*(entry->extension[0]);
- }
- if (match_weight>best_weight){
- // record the new longest entry
- found_pos=posndx;
- best_weight=match_weight;
- }
- }
- }
- posndx+=entry->total_length;
- }
-
- if (best_weight==-1)
- return icPrefNotFoundErr;
-
- return ICRGetMapEntry(inst,entries,found_pos,entry);
- }
-
- // High Level Mapping Routines
-
- ICError ICRMapFilename(ICRRecord* inst,StringPtr filename,ICMapEntry* entry){
- ICError err;
- Handle entries;
- ICAttr junk_attr;
-
- if (filename[0]==0)
- return paramErr;
-
- err=ICRGetPrefHandle(inst,kICMapping,&junk_attr,&entries);
- if (err==noErr){
- err=ICRMapEntriesFilename(inst,entries,filename,entry);
- DisposeHandle(entries);
- }
-
- return err;
- }
-
- ICError ICRMapTypeCreator(ICRRecord* inst,OSType fType,OSType fCreator,StringPtr filename,ICMapEntry* entry){
- ICError err;
- Handle entries;
- ICAttr junk_attr;
-
- err=ICRGetPrefHandle(inst,kICMapping,&junk_attr,&entries);
- if (err==noErr){
- err=ICRMapEntriesTypeCreator(inst,entries,fType,fCreator,filename,entry);
- DisposeHandle(entries);
- }
-
- return err;
- }
-
-
-
-
-
-
-
-
-
-
-